package com.splant.android.smart.UILevel.Community;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.Global;
import com.splant.android.smart.ModelLevel.sPlantRequestParams;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_StringMatches_Util;
import org.apache.http.Header;
import org.json.JSONObject;

public class Garden_UpdatePsw_Activity extends Activity implements View.OnClickListener {

    private Button btn_goBack;
    private EditText edt_oldPsw;
    private EditText edt_newPsw;
    private EditText edt_reNewPsw;
    private Button btn_enter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.garden_updatepsw_activity);
        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        edt_oldPsw = (EditText) findViewById(R.id.edt_oldPsw);
        edt_newPsw = (EditText) findViewById(R.id.edt_newPsw);
        edt_reNewPsw = (EditText) findViewById(R.id.edt_reNewPsw);
        btn_enter = (Button) findViewById(R.id.btn_enter);
        btn_goBack.setOnClickListener(this);
        btn_enter.setOnClickListener(this);
    }

    private sPlantAsyncHttpClient sPlantClient;

    private void updatePsw() {

        String oldPsw = edt_oldPsw.getText().toString();
        String newPsw = edt_newPsw.getText().toString();
        String reNewPsw = edt_reNewPsw.getText().toString();

        if (oldPsw.equals("")) {
            Toast.makeText(this, "请输入原密码！", Toast.LENGTH_LONG).show();
        } else if (newPsw.equals("")) {
            Toast.makeText(this, "请输入新密码！", Toast.LENGTH_LONG).show();
        } else if (newPsw.length() < 6 || System_StringMatches_Util.isLetterAndNumber(newPsw)==false) {
            Toast.makeText(this, "新密码格式不正确！", Toast.LENGTH_LONG).show();
        } else if (reNewPsw.equals("")) {
            Toast.makeText(this, "请确认新密码！", Toast.LENGTH_LONG).show();
        } else if (newPsw.equals(reNewPsw) == false) {
            Toast.makeText(this, "两次输入的密码不一致，请重新输入！", Toast.LENGTH_LONG).show();
        } else {

            sPlantClient = sPlantAsyncHttpClient.getInstance();

            sPlantRequestParams params = new sPlantRequestParams();
            params.putParams("userId", Global.user.getUserId());
            params.putParams("oldPsw", oldPsw);
            params.putParams("newPsw", newPsw);

            sPlantClient.post(this, sPlantAsyncHttpClient.UPDATEPSW, params, new sPlantJsonHttpResponseHandler() {

                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            try
                            {
                                if (response.getInt("succeed") == 1) {
                                    Toast.makeText(Garden_UpdatePsw_Activity.this, "修改成功！", Toast.LENGTH_LONG).show();
                                }
                                else if (response.getInt("succeed") ==0)
                                {
                                    String msg=response.getString("message");
                                    Toast.makeText(Garden_UpdatePsw_Activity.this, msg, Toast.LENGTH_LONG).show();
                                }
                            }
                            catch (Exception e)
                            {

                            }
                        }
                        @Override
                        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                            Toast.makeText(Garden_UpdatePsw_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();
                        }
                    }
            );


        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_goBack:
                finish();
                break;
            case R.id.btn_enter:
                updatePsw();
                break;
        }
    }


}
